<template>
  <div class="execution">
    <basic-container>
      <el-tabs v-model="activeName" @tab-click="handleClickTab">
        <el-tab-pane label="商家审核" name="first"></el-tab-pane>
        <el-tab-pane label="正常商家" name="second"></el-tab-pane>
        <el-tab-pane label="异常商家" name="third"></el-tab-pane>
        <el-tab-pane label="不通过" name="fourth"></el-tab-pane>

        <el-card class="filter-container" shadow="never">
          <div style="margin-top: 10px">
            <el-form :inline="true" size="small" label-width="140px">
              <el-form-item label="商家名称：">
                <el-select v-model="listQuery.storeId" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.storeId"
                    :label="item.storeName"
                    :value="item.storeId"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button @click="getList" type="primary" size="small"
                  >查询结果</el-button
                >
                <el-button @click="handleResetSearch" size="small">
                  重置
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-card>

        <div style="margin-top: 10px">
          <el-table
            :data="list"
            border
            :header-cell-style="{ color: '#4D4D4D', background: '#fafafa' }">

            <el-table-column label="商家名称" align="center" prop="storeName"></el-table-column>
            <el-table-column v-if="listQuery.status !== 1" label="申请时间" align="center" prop="createTime"></el-table-column>
            <el-table-column label="操作人" align="center" prop="editName"></el-table-column>
            <el-table-column
              label="操作时间"
              align="center"
              prop="editTime"
            ></el-table-column>
            <el-table-column
              v-if="listQuery.status === 1"
              label="开店时间"
              align="center"
              prop="adoptTime"
            ></el-table-column>
            <el-table-column
              v-if="listQuery.status === 3"
              label="锁定时间"
              align="center"
              prop="examineTime"
            ></el-table-column>
            <el-table-column
              v-if="listQuery.status === 1"
              label="是否短信提醒"
              align="center">
              <template slot-scope="scope">
                <el-switch
                  @change="handleShowStatusChange(scope.$index, scope.row)"
                  :active-value="1"
                  :inactive-value="2"
                  v-model="scope.row.isSmsRemind">
                </el-switch>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  size="small"
                  v-if="listQuery.status === 2"
                  @click="handleExamine(scope.$index, scope.row)"
                  type="text"
                  icon="el-icon-edit"
                  >审核</el-button
                >
                <el-button
                  size="small"
                  v-if="listQuery.status !== 2"
                  @click="handleDetails(scope.$index, scope.row)"
                  type="text"
                  >详情</el-button>
                <el-button
                  size="small"
                  v-if="listQuery.status === 1"
                  @click="handleLockingManage(scope.$index, scope.row)"
                  type="text"
                  icon="el-icon-edit"
                  >锁定</el-button>

                <el-button
                  size="small"
                  v-if="listQuery.status === 1"
                  @click="handleReset(scope.$index, scope.row)"
                  type="text"
                  >重置密码</el-button>

                <el-button
                  size="small"
                  v-if="listQuery.status === 3"
                  @click="handleUnlockingManage(scope.$index, scope.row)"
                  type="text"
                  icon="el-icon-edit"
                  >解锁</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="paginat                                                                                                                                     ion-container">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            layout="total, sizes,prev, pager, next,jumper"
            :page-size="listQuery.size"
            :page-sizes="[5, 10, 15]"
            :current-page.sync="listQuery.current"
            :total="total"
          >
          </el-pagination>
        </div>
      </el-tabs>
    </basic-container>

    <el-dialog title="锁定" :visible.sync="verty.dialogVisible" width="40%">
      <el-form :model="verty" ref="verty" :rules="rules" label-width="150px">
        <el-form-item label="锁定原因：" prop="message">
          <el-input v-model="verty.message"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="verty.dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditVConfirm('verty')"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import "@/styles/index.scss";
import { fetchList, getStore, storeLoke,storeUnlocking, resetPwd, setUpRemind } from "@/api/rms/rmsstore";

const defaultListQuery = {
  current: 1,
  size: 10,
  storeId: "",
  status: 2, //状态：2--待审核；1--通过；3--异常商家；4--不通过
};
export default {
  data() {
    return {
      activeName: "first",
      options: [],
      storeId: null,
      listQuery: Object.assign({}, defaultListQuery),
      list: [],
      total: 0,
      tabValueMap: {
        first: 2,
        second: 1,
        third: 3,
        fourth: 4,
      },
      verty: {
        dialogVisible: false,
        message: null,
      },
      rules: {
        message: [{ required: true, message: "请填写原因！", trigger: "blur" }],
      },
      verFrom: {}
    };
  },
  created() {
    this.getList();
    this.getStoreList();
  },
  filters: {},
  methods: {
    handleClickTab(tab, event) {
      this.listQuery.status = this.tabValueMap[this.activeName];
      this.listQuery.storeId = "";
      this.listQuery.current = 1;
      this.getList();
      this.getStoreList();
    },
    getStoreList() {
      getStore({ status: this.listQuery.status }).then((res) => {
        this.options = res.data.data;
      });
    },
    handleSizeChange(val) {
      this.listQuery.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.current = val;
      this.getList();
    },
    getList() {
      fetchList(this.listQuery).then((res) => {
        this.list = res.data.data.records;
        this.total = res.data.data.total;
      });
    },
    handleResetSearch() {
      this.listQuery.storeId = "";
      this.getList();
    },

    handleExamine(index, row) {
      this.$router.push({
        path: "/rms/audit",
        query: { storeId: row.storeId },
      });
    },
    handleDetails(index, row) {
      this.$router.push({
        path: "/rms/details",
        query: { storeId: row.storeId },
      });
    },
    handleLockingManage(index, row) {
      this.verty.dialogVisible = true;
      this.verFrom = row
    },
    handleEditVConfirm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            storeId: this.verFrom.storeId,
            status: 3,
            message: this.verty.message,
          };
          storeLoke(params).then((res) => {
            this.verty.dialogVisible = false;
            if (res.data.code == 200) {
              this.$message({
                message: res.data.msg,
                type: "success",
                duration: 1000,
              });
              this.getList();
            } else {
              this.$message({
                message: res.data.msg,
                type: "warning",
                duration: 1000,
              });
            }
          });
        }
      });
    },
    handleUnlockingManage(index, row) {
      const that = this;
      that.$confirm("是否确认解锁" + row.storeName, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        storeUnlocking({storeId: row.storeId}).then(res => {
          if (res.data.code == 200) {
            that.$message({
              message: res.data.msg,
              type: "success",
              duration: 1000,
            });
            that.getList();
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning",
              duration: 1000,
            });
          }
        })
      })
    },
    handleReset(index, row) {
      const that = this;
      that.$confirm("确认重置" + row.storeName + "店铺密码？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        resetPwd({storeId: row.storeId}).then(res => {
          if (res.data.code == 200) {
            that.$message({
              message: res.data.msg,
              type: "success",
              duration: 1000,
            });
            that.getList();
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning",
              duration: 1000,
            });
          }
        })
      })
    },
    handleShowStatusChange(index,row) {
      const that = this;
      setUpRemind({
        storeId: row.storeId,
        isSmsRemind: row.isSmsRemind
      }).then(res => {
        if (res.data.code == 200) {
            that.$message({
              message: res.data.msg,
              type: "success",
              duration: 1000,
            });
            that.getList();
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning",
              duration: 1000,
            });
          }
      })
    }
  }
};
</script>
